<?php
namespace app\fruit\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;

class StudentFruitReg extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$where = [];
		$termId = input("termId");
		$grade = input("grade");
		$level = input("获奖级别");
		$rank = input("获奖等第");
		$studentName = input("studentName");
		$page = input("page");
		$rows = input("rows");
		if($termId) $where["学期ID"] = $termId;
		if($grade) $where["年级"] = $grade;
		if($level) $where["获奖级别"] = $level;
		if($rank) $where["获奖等第"] = $rank;
		if($studentName) $where["姓名"] = array("like",$studentName);
		$result = Db::table('学生成果')->alias('a')
				  ->join('学期名称 b','a.学期ID = b.ID','LEFT')
				  ->join('学生信息 c','a.学生ID=c.ID','LEFT')
				  ->field("a.ID,a.竞赛名称,a.参赛组别,a.参赛子项目,a.作品名称,a.年级,a.班别,a.获奖级别,a.获奖等第,a.获奖时间,a.授奖单位,a.指导教师,a.加分,a.备注,a.学期ID,a.学生ID,b.学期名称,c.姓名")
				  ->where($where)
				  ->order("学期ID desc,ID desc")
				  ->page($page, $rows)
		          ->select();
		$count = Db::table('学生成果')->alias('a')
				  ->join('学期名称 b','a.学期ID = b.ID','LEFT')
				  ->join('学生信息 c','a.学生ID=c.ID','LEFT')
				  ->where($where)
				  ->count();
		$grid['rows'] = selectResult($result);
        $grid['total'] = $count;
		return $grid;
	}

	public function exportExcel() {
		$where = [];
		$termId = input("termId");
		$grade = input("grade");
		$level = input("获奖级别");
		$rank = input("获奖等第");
		$studentName = input("studentName");
		if($termId) $where["学期ID"] = $termId;
		if($grade) $where["年级"] = $grade;
		if($level) $where["获奖级别"] = $level;
		if($rank) $where["获奖等第"] = $rank;
		if($studentName) $where["姓名"] = array("like",$studentName);
		$result = Db::table('学生成果')->alias('a')
				  ->join('学期名称 b','a.学期ID = b.ID','LEFT')
				  ->join('学生信息 c','a.学生ID=c.ID','LEFT')
				  ->field("a.ID,a.竞赛名称,a.参赛组别,a.参赛子项目,a.作品名称,a.年级,a.班别,a.获奖级别,a.获奖等第,a.获奖时间,a.授奖单位,a.指导教师,a.加分,a.备注,a.学期ID,a.学生ID,b.学期名称,c.姓名")
				  ->where($where)
				  ->order("学期ID desc,ID desc")
		          ->select();
		$header = array('学期名称'=>'学期名称','姓名'=>'姓名','竞赛名称'=>'竞赛名称','参赛组别'=>'参赛组别','参赛子项目'=>'参赛子项目','作品名称'=>'作品名称','年级'=>'年级','班别'=>'班别','获奖级别'=>'获奖级别','获奖等第'=>'获奖等第','获奖时间'=>'获奖时间','授奖单位'=>'授奖单位','指导教师'=>'指导教师','加分'=>'加分','备注'=>'备注');
		Excel::toExcel(selectResult($result),'学生成果',$header);
		exit;
	}
	public function addUpdate() {
		$param = request()->param();
		try {
			if(array_key_exists("年级", $param)) {
				$param["年级"] = getCommonGradeName($param["年级"]);
			}
			if(array_key_exists("ID", $param)) {
				update('学生成果', $param);
			}else {
				insert('学生成果', $param);
			}
			return '000';
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return '999';
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::name('学生成果')->where('ID','in',explode(",", $delIds))->delete();
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

}
